package acm.深度优先遍历;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class N {

    static int[][] directions={
            {0,1},
            {0,-1},
            {1,0},
            {-1,0},
    };
    public static int[][] fun(int row, int col, List<int[]>path,boolean[][] maze){
        if (row>=maze.length||row<0||col>=maze[0].length||col<0||!maze[row][col]){
            return null;
        }
        if (row==4&&col==4){
            path.add(new int[]{4,4});
            path.toArray();
            int[][] clone = path.toArray(new int[][]{});
            path.remove(path.size()-1);
            return clone;
        }
        maze[row][col]=false;
        path.add(new int[]{row,col});
        int[][] res=null;
        for (int[] d : directions) {
            int[][] to = fun(row + d[0], col + d[1], path, maze);
            if (res==null||(to!=null&&to.length<res.length)){
                res=to;
            }
        }
        path.remove(path.size()-1);
        maze[row][col]=true;
        return res;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        boolean[][] maze=new boolean[5][5];
        for (boolean[] m : maze) {
            for (int i = 0; i < m.length; i++) {
                m[i]=sc.nextInt()==0;
            }
        }
        int[][] fun = fun(0, 0, new ArrayList<>(), maze);
        for (int[] ints : fun) {
            System.out.println("("+ ints[0] +", "+ints[1]+")");
        }
    }
}
